#include "StdAfx.h"
#include "Timer.h"

u32 CTimer::m_lastTime = 0;
u32 CTimer::m_firstTime = 0;
u32 CTimer::m_frameTime = 0;
u32 CTimer::m_fpsx10 = 0;
u32 CTimer::m_nrFrames;

void CTimer::init()
{
	m_lastTime = getCurrentTime();
	m_firstTime = getCurrentTime();
	m_frameTime = 0;
	m_fpsx10 = 0;
	m_nrFrames = 0;
	
	OS_InitTick();
}

void CTimer::update()
{
	u32 t = getCurrentTime();
	
	m_frameTime = t-m_lastTime;
	m_nrFrames ++;
	
	if (t-m_firstTime>=1000)
	{
		m_fpsx10 = (m_nrFrames * 10 * 1000) / (t-m_firstTime);
		m_firstTime = t;
		m_nrFrames = 0;
	}
	
	m_lastTime = t;	
}

u32 CTimer::getCurrentTime()
{
	return (u32)OS_TicksToMilliSeconds( OS_GetTick() );
}

u32 CTimer::getFrameTime()
{
	return m_lastTime;
}

u32 CTimer::getFPSx10()
{
	return m_fpsx10;
}

void CTimer::limit(int ms)
{
	do
	{
		OS_Sleep(1);
	}
	while (CTimer::getCurrentTime() - m_lastTime < ms);			
}